<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-search"
          @click="search"
          >刷新</el-button
        >
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table
      size="small"
      :data="roleDate"
      highlight-current-row
      v-loading="loading"
      border
      element-loading-text="拼命加载中"
      style="width: 100%"
    >
      <el-table-column
        align="rolename"
        sortable
        prop="rolename"
        label="角色名称"
        width="110"
      >
      </el-table-column>
      <el-table-column
        align="center"
        sortable
        prop="description"
        label="角色描述"
        width="200"
      >
      </el-table-column>
      <el-table-column label="菜单权限" width="180">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <span>{{ scope.row.meuns }}</span>
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">详情</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        sortable
        prop="createtime"
        label="创建时间"
        width="200"
      >
      </el-table-column>
      <el-table-column
        align="center"
        sortable
        prop="updatetime"
        label="更新时间"
        width="200"
      >
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="300">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >菜单权限</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination
      v-bind:child-msg="pageparm"
      @callFather="callFather"
    ></Pagination>
    <!-- 编辑界面 -->
    <el-dialog
      :title="title"
      :visible.sync="editFormVisible"
      width="50%"
      @click="closeDialog('edit')"
    >
      <div style="text-align: center">
        <el-transfer
          style="text-align: left; display: inline-block"
          v-model="value"
          :render-content="renderFunc"
          :titles="['不可视菜单组', '可视菜单组']"
          :button-texts="['到左边', '到右边']"
          :format="{
            noChecked: '${total}',
            hasChecked: '${checked}/${total}',
          }"
          @change="handleChange"
          :data="data"
        >
        </el-transfer>
      </div>
      <!-- 底部按钮 -->
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog('edit')">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  roleList,
  getmenusbyroleid,
  updaterolemenus,
} from "../../api/sysMG";
import { getMenulist }from "../../api/basisMG";
import Pagination from "../../components/Pagination";
export default {
  data() {
    const generateData = (_) => {
      const data = [];
      getMenulist().then((res) => {
        if (res.status == 200) {
          this.data = res.data;
        }
      });
      return data;
    };
    return {
      mapObject: new Map(),
      data: generateData(),
      value: [],
      renderFunc(h, option) {
        return (
          <span>
            {option.key} - {option.label}
          </span>
        );
      },
      roleid: 0,
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      menuAccessshow: false, //控制数据权限显示与隐藏
      title: "编辑菜单权限",
      formInline: {
        page: 1,
        size: 10,
      },
      roleDate: [], //角色数据
      //参数role
      saveroleId: "",
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10,
      },
    };
  },
  // 注册组件
  components: {
    Pagination,
  },
  watch: {},
  /**
   * 创建完毕
   */
  created() {
    this.getdata(this.formInline);
  },
  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    // 获取角色列表
    getdata(parameter) {
      this.loading = true;
      roleList(parameter).then((res) => {
        this.loading = false;
        if (res.status == 200) {
          this.roleDate = res.data;
          // 分页赋值
          this.pageparm.currentPage = this.formInline.page;
          this.pageparm.pageSize = this.formInline.size;
          this.pageparm.total = res.count;
        } else {
          this.$message({
            type: "info",
            message: res.statusInfo.message,
          });
        }
      });
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage;
      this.formInline.size = parm.pageSize;
      this.getdata(this.formInline);
    },
    // 搜索事件
    search() {
      this.getdata(this.formInline);
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.editFormVisible = true;
      this.roleid = row.id;
      // 重新请求拿数据
      getmenusbyroleid({ id: row.id }).then((res) => {
        if (res.status == 200) {
          this.value = res.data;
        }
      });
    },
    // 关闭编辑弹出框
    closeDialog(dialog) {
      if (dialog == "edit") {
        this.editFormVisible = false;
        this.getdata(this.formInline);
      }
    },
    // 更改权限
    handleChange(value, direction, movedKeys) {
      if (direction == "left") {
        // 添加到此用户的菜单栏中
      } else {
        // 从此用户的菜单栏中删除
      }
      updaterolemenus({
        id: this.roleid,
        direction: direction,
        movedKeys: movedKeys
      }).then((res) => {
        if (res.status == 200) {
          this.$message({
            type: "success",
            message: "操作成功",
          });
        }else{
          // 提示后关闭编辑页
          this.$message({
            type: "error",
            message: "操作失败",
          });
          closeDialog("edit");
        }
      });
    },
  },
};
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
.transfer-footer {
  margin-left: 20px;
  padding: 6px 5px;
}
</style>